package com.blockmeta.bbs.businesslibrary.net.request;

import androidx.annotation.Nullable;
import androidx.annotation.StringDef;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.internal.Util;
import okio.BufferedSink;

/**
 * @author AirThor
 * @date 2019/4/29.
 * 对php统一转发的请求体
 */
public abstract class ForwardApiRequest extends RequestBody {
    /**
     * @param apiKey 请求转发接口的key 用来识别转发的是哪个接口
     * @param body   请求转发接口的参数json
     * @return 请求体
     */
    public static ForwardApiRequest create(@ApiKey.Kind String apiKey, JSONObject body) {
        JSONObject root = new JSONObject();
        try {
            root.put("apiKey", apiKey);
            root.put("paramJson", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String content = root.toString();
        MediaType contentType = MediaType.parse("application/json");
        Charset charset = Util.UTF_8;
        if (contentType != null) {
            charset = contentType.charset();
            if (charset == null) {
                charset = Util.UTF_8;
                contentType = MediaType.parse(contentType + "; charset=utf-8");
            }
        }
        byte[] bytes = content.getBytes(charset);
        return create(contentType, bytes);
    }

    public static class ApiKey {
        /**
         * 活动报名
         */
        public static final String ACTIVITY_APPLY = "8btc.php.pay.activityApply";
        /**
         * app支付下单
         */
        public static final String APP_PAY = "8btc.php.pay.appPay";
        /**
         * 下单
         */
        public static final String ORDER = "8btc.php.pay.order";
        /**
         * app支付回调验证
         */
        public static final String ALIPAY_CALLBACK = "8btc.php.pay.appAlipaySuccess";


        /**
         * 关注作者文章
         */
        public static final String FOLLOWS_AUTHOR_ARTICLE = "8btc.php.news.followAuthorArticles";
        /**
         * 推荐作者
         */
        public static final String FOLLOWS_AUTHOR_RECOMMEND = "8btc.php.news.rankingAuthor";
        /**
         * 海盗号最新文章全部http://test_zx.api.8btc.com/bbt_api/news/app-list
         */
        public static final String VIKING_NEWS_LIST = "8btc.php.news.appList";
        /**
         * 关注某个人http://test_zx.api.8btc.com/bbt_api/news/app-authorfollow
         */
        public static final String FOLLOW_AUTHOR = "8btc.php.news.appAuthorFollow";
        /**
         * 查找月份上的作者 （按月查找）8btc.php.news.ranking
         */
        public static final String MONTHS_AUTHOR = "8btc.php.news.ranking";
        /**
         * 检查是否关注
         */
        public static final String CHECK_AUTHOR_FOLLOW = "8btc.php.news.appCheckFollow";
        /**
         * 获取当前用户的关注作者列表
         */
        public static final String GET_USERFOLLOWS_AUTHOR = "8btc.php.news.followList";

        /**
         * 扫码登录设备信息
         */
        public static final String Achieve_DeviceInfo = "8btc.php.login.oauth.clientInfo";

        /**
         *
         */
        public static final String Bind2ChainNode = "8btc.php.login.oauth.bindCode";



        /**
         * 发帖
         */
        public static final String POST = "8btc.php.appForum.publish.newThread";


        @StringDef({ACTIVITY_APPLY, APP_PAY, ORDER, ALIPAY_CALLBACK, FOLLOWS_AUTHOR_ARTICLE, FOLLOWS_AUTHOR_RECOMMEND
                           , VIKING_NEWS_LIST, FOLLOW_AUTHOR, MONTHS_AUTHOR, CHECK_AUTHOR_FOLLOW, GET_USERFOLLOWS_AUTHOR
        ,Achieve_DeviceInfo,Bind2ChainNode,POST})
        @interface Kind {
        }
    }

    /**
     * Returns a new request body that transmits {@code content}.
     */
    public static ForwardApiRequest create(final @Nullable MediaType contentType, final byte[] content) {
        return create(contentType, content, 0, content.length);
    }

    /**
     * Returns a new request body that transmits {@code content}.
     */
    public static ForwardApiRequest create(final @Nullable MediaType contentType, final byte[] content,
                                           final int offset, final int byteCount) {
        if (content == null) {throw new NullPointerException("content == null");}
        Util.checkOffsetAndCount(content.length, offset, byteCount);
        return new ForwardApiRequest() {
            @Override
            public @Nullable
            MediaType contentType() {
                return contentType;
            }

            @Override
            public long contentLength() {
                return byteCount;
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                sink.write(content, offset, byteCount);
            }
        };
    }
}
